package com.crazybytex.modbus.util;

import com.crazybytex.modbus.Constant;
import com.crazybytex.modbus.beans.ModBusResponse;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersResponse;

import java.util.Arrays;

/**
 * modbus 工具类
 *
 * @author 程序员潇然 crazybytex.com
 */
public class ModbusUtil {


    /**
     * 读输入寄存器
     * 功能码 READ_INPUT_REGISTERS=4
     * ModBusResponse 子类 可以通过实现parse方法解析字节数组   也可以单纯地使用ModBusResponse 通过对返回的字节进行处理,这样不需要绑定子类,可以直接调用
     *
     * @param master
     * @param slaveId
     * @param start
     * @param len
     * @param modBusResponse
     */
    public static byte[] readInputRegisters(ModbusMaster master, int slaveId, int start, int len, ModBusResponse modBusResponse) {
        try {
            ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId, start, len);
            System.out.println("request data:"+request);

            ReadInputRegistersResponse response = (ReadInputRegistersResponse) master.send(request);

            if (response.isException()) {
                modBusResponse.setCode(Constant.RET_CODE_FAILED);
                modBusResponse.setMsg(response.getExceptionMessage());
                return null;
            }
            System.out.println("received bytes:"+ByteStreamUtils.bytesToHex(response.getData()));
            System.out.println("received short array:"+Arrays.toString(response.getShortData()));
            modBusResponse.parse(response.getData());
            System.out.println("final response data:"+modBusResponse);
            System.out.println();
            return response.getData();


        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
        return null;
    }


/**
 * 读取离散输入
 * READ_DISCRETE_INPUTS = 2
 *
 * @param master
 * @param slaveId
 * @param start
 * @param len
 *//*

    public static void readDiscreteInput(ModbusMaster master, int slaveId, int start, int len) {
        try {
            ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, start, len);
            ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(Arrays.toString(response.getBooleanData()));
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }


    */
/**
 * 读保持寄存器
 *
 * @param master
 * @param slaveId
 * @param start
 * @param len
 *//*

    public static void readHoldingRegisters(ModbusMaster master, int slaveId, int start, int len) {
        try {
            ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len);
            ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(Arrays.toString(response.getShortData()));
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }


    */
/**
 * 读单个线圈
 *
 * @param master
 * @param slaveId
 * @param start
 * @param len
 *//*

    public static void readCoil(ModbusMaster master, int slaveId, int start, int len) {
        try {
            ReadCoilsRequest request = new ReadCoilsRequest(slaveId, start, len);
            ReadCoilsResponse response = (ReadCoilsResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(Arrays.toString(response.getBooleanData()));
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }


    */
/**
 * 写单个线圈
 *
 * @param master
 * @param slaveId
 * @param offset
 * @param value
 *//*

    public static void writeCoil(ModbusMaster master, int slaveId, int offset, boolean value) {
        try {
            WriteCoilRequest request = new WriteCoilRequest(slaveId, offset, value);
            WriteCoilResponse response = (WriteCoilResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }


    public static void writeRegister(ModbusMaster master, int slaveId, int offset, int value) {
        try {
            WriteRegisterRequest request = new WriteRegisterRequest(slaveId, offset, value);
            WriteRegisterResponse response = (WriteRegisterResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

    public static void readExceptionStatus(ModbusMaster master, int slaveId) {
        try {
            ReadExceptionStatusRequest request = new ReadExceptionStatusRequest(slaveId);
            ReadExceptionStatusResponse response = (ReadExceptionStatusResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(response.getExceptionStatus());
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

    public static void reportSlaveId(ModbusMaster master, int slaveId) {
        try {
            ReportSlaveIdRequest request = new ReportSlaveIdRequest(slaveId);
            ReportSlaveIdResponse response = (ReportSlaveIdResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(Arrays.toString(response.getData()));
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

    public static void writeCoils(ModbusMaster master, int slaveId, int start, boolean[] values) {
        try {
            WriteCoilsRequest request = new WriteCoilsRequest(slaveId, start, values);
            WriteCoilsResponse response = (WriteCoilsResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

    public static void writeRegisters(ModbusMaster master, int slaveId, int start, short[] values) {
        try {
            WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
            WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

    public static void writeMaskRegister(ModbusMaster master, int slaveId, int offset, int and, int or) {
        try {
            WriteMaskRegisterRequest request = new WriteMaskRegisterRequest(slaveId, offset, and, or);
            WriteMaskRegisterResponse response = (WriteMaskRegisterResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }
*/


}
